package sg.edu.nus.comp.icreate.ivletalk.forum;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

import sg.edu.nus.comp.icreate.ivletalk.utils.xml.AbstractXmlParser;

public class ForumXmlParser extends AbstractXmlParser {
	private static final String TAG_HEADINGS = "Headings";
	private static final String TAG_HEADING = "Data_Forum_Heading";
	protected static final String TAG_THREAD = "Data_Forum_Thread";
	protected static final String TAG_NUM_REPLIES = "NumOfReplies";
	protected static final String TAG_POST_BODY = "PostBody";
	protected static final String TAG_POST_TITLE = "PostTitle";
	protected static final String TAG_DATE = "PostDate";
	protected static final Object TAG_POSTER = "Name";
	protected static final Object TAG_TITLE = "Title";

	public ForumXmlParser(InputStream input) throws XmlPullParserException {
		super(input);
	}

	public ForumXmlParser() {
		super();
	}

	@Override
	public Object getResult() {
		int eventType;
		List<Heading> headings = new ArrayList<Heading>();
		try {
			goToTag(TAG_HEADINGS, XmlPullParser.START_TAG);
			Heading heading = null;
			ForumThread thread = null;
			do {
				eventType = parser.getEventType();
				if (eventType == XmlPullParser.START_TAG && TAG_HEADING.equals(parser.getName())) {
					heading = new Heading();
				} else if (eventType == XmlPullParser.START_TAG && TAG_ID.equals(parser.getName())) {
					parser.next();
					if (thread != null)
						thread.setId(parser.getText());
					else if (heading != null)
						heading.setId(parser.getText());
				} else if (eventType == XmlPullParser.START_TAG && TAG_THREAD.equals(parser.getName())) {
					if (thread == null) {
						thread = new ForumThread();
						heading.getThreads().add(thread);
					} else {
						ForumThread temp = new ForumThread(thread);
						thread.getReplies().add(temp);
						thread = temp;
					}
				} else if (eventType == XmlPullParser.START_TAG && TAG_NUM_REPLIES.equals(parser.getName())) {
					parser.next();
					thread.setNumReplies(Integer.parseInt(parser.getText()));
				} else if (eventType == XmlPullParser.START_TAG && TAG_POST_BODY.equals(parser.getName())) {
					parser.next();
					thread.setBody(parser.getText() == null ? "" : parser.getText());
				} else if (eventType == XmlPullParser.START_TAG && TAG_POST_TITLE.equals(parser.getName())) {
					parser.next();
					thread.setTitle(parser.getText());
				} else if (eventType == XmlPullParser.START_TAG && TAG_DATE.equals(parser.getName())) {
					parser.next();
					String dates[] = parser.getText().split("T");
					String date[] = dates[0].split("-");
					String time[] = dates[1].split(":");
					Calendar calendar = new GregorianCalendar(Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]),
							Integer.parseInt(time[0]), Integer.parseInt(time[1]));
					thread.setDate(calendar);
				} else if (eventType == XmlPullParser.START_TAG && TAG_POSTER.equals(parser.getName())) {
					parser.next();
					thread.setPoster(parser.getText());
				} else if (eventType == XmlPullParser.START_TAG && TAG_TITLE.equals(parser.getName()) && thread == null) {
					parser.next();
					heading.setTitle(parser.getText());
				} else if (eventType == XmlPullParser.END_TAG && TAG_THREAD.equals(parser.getName())) {
					thread = thread.getParent();
				} else if (eventType == XmlPullParser.END_TAG && TAG_HEADING.equals(parser.getName())) {
					Collections.sort(heading.getThreads());
					headings.add(heading);
				}
				parser.next();
			} while (parser.getEventType() != XmlPullParser.END_TAG || !(TAG_HEADINGS.equals(parser.getName()) || TAG_RESULT.equals(parser.getName())));

		} catch (Exception e) {
			Log.e("error", "Error in parse xml", e);
		}
		return headings;
	}

}
